<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @enter="handleQuery"
        label-width="65px"
      >
        <el-form-item label="服务名称" prop="serverName">
          <common-input
            v-model="queryParams.serverName"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="IP" prop="serverIp">
          <common-input
            v-model="queryParams.serverIp"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="端口" prop="serverPort">
          <common-input
            v-model="queryParams.serverPort"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="健康状态" prop="status">
          <el-select v-model="queryParams.status">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="健康" value="0"></el-option>
            <el-option label="不健康" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>

    <common-table v-loading="loading" slot="content" :data="serverList">
      <el-table-column
        label="服务名称"
        align="left"
        prop="serverName"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        label="IP"
        align="left"
        prop="serverIp"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column label="端口" align="left" prop="serverPort"  width="100"/>
      <el-table-column label="健康状态" align="left" prop="status"  width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="success">健康 </el-tag>
          <el-tag v-if="scope.row.status === '1'" type="warning">不健康</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="异常信息"
        align="left"
        prop="exceptionInfo"
        show-overflow-tooltip
      />
      <el-table-column label="操作" align="left" width="60px">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)"
            >详情
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <server-detail
      v-if="serverDetailShow"
      @back="serverDetailShow = false"
      :peakObj="peakObj"
      :uri="uri"
      :serverName="serverName"
    ></server-detail>
  </common-list>
</template>

<script>
import { fetchList } from "@/api/management/monitor/server";
import { fetchMonitorList } from "@/api/system/monitor";
import ServerDetail from "./components/serverDetail.vue";
import BaseList from "@/mixins/BaseList";
export default {
  mixins: [BaseList],
  components: {
    ServerDetail,
  },
  data() {
    return {
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serverIp: null,
        isDefault: null,
        serverPort: null,
        status: null,
      },
      total: 0,
      serverList: [],
      loading: false,
      serverDetailShow: false,
      uri: "",
      serverName: "",
      peakObj: {
        maxCPU: 0, //cpu
        maxMemory: 0, //内存
        maxDisk: 0, //磁盘
      },
    };
  },
  created() {
    this.getList();
    this.getMonitorThreshold();
  },
  methods: {
    getList() {
      this.loading = true;
      fetchList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.serverList = res.rows;
            this.total = res.total;
          } else {
            this.serverList = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .catch(() => {
          this.serverList = [];
          this.total = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    getMonitorThreshold() {
      fetchMonitorList({ pageNum: 1, pageSize: 10, ruleType: 0 }).then(
        (res) => {
          if (res && res.code === 200) {
            this.peakObj = {
              maxCPU: +res.rows[0].cpuLimit || 0, //cpu
              maxMemory: +res.rows[0].memLimit || 0, //内存
              maxDisk: +res.rows[0].diskLimit || 0, //磁盘
            };
          } else {
            this.peakObj = {
              maxCPU: 0, //cpu
              maxMemory: 0, //内存
              maxDisk: 0, //磁盘
            };
          }
        }
      );
    },

    handleView(row) {
      this.uri = row.uri;
      this.serverName = row.serverName;
      this.serverDetailShow = true;
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
  },
};
</script>

<style lang="scss"></style>
